package com.dhcc.bpm.modules.bbs.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.bbs.entity.TopicUser;
import com.dhcc.bpm.modules.bbs.service.ITopicUserService;
import com.dhcc.bpm.modules.bbs.vo.TopicUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TopicUserController
 * @Description 论坛板块用户关联表 控制器
 * @Author daina
 * @Date 2020/3/6 15:55
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping("bpm-bbs/topicuser")
@Api(value = "论坛板块用户关联表", tags = "论坛板块用户关联表接口")
public class TopicUserController extends BladeController {

	private ITopicUserService topicUserService;

	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 15:58 2020/3/6
	 * @param topicUser
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.bbs.entity.TopicUser>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入topicUser")
	@Log(title = "查看详情",businessType = BusinessType.SELECT)
	@ApiLog("查看详情")
	public R<TopicUser> detail(TopicUser topicUser) {
		TopicUser detail = topicUserService.getOne(Condition.getQueryWrapper(topicUser));
		return R.data(detail);
	}

	/**
	 * @Description 分页 论坛板块用户关联表
	 * @Author daina
	 * @Date 15:58 2020/3/6
	 * @param topicUser
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.bbs.entity.TopicUser>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页 论坛板块用户关联表", notes = "传入topicUser")
	@ApiLog("分页 论坛板块用户关联表")
	@Log(title = "分页 论坛板块用户关联表",businessType = BusinessType.SELECT)
	public R<IPage<TopicUser>> list(@ApiIgnore @RequestParam Map<String, Object>  topicUser, Query query) {
		IPage<TopicUser> pages = topicUserService.page(Condition.getPage(query), Condition.getQueryWrapper(topicUser,TopicUser.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 论坛板块用户关联表
	 * @Author daina
	 * @Date 15:58 2020/3/6
	 * @param topicUser
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.bbs.vo.TopicUserVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiLog("自定义分页 论坛板块用户关联表")
	@ApiOperation(value = "自定义分页 论坛板块用户关联表", notes = "传入topicUser")
	@Log(title = "自定义分页 论坛板块用户关联表",businessType = BusinessType.SELECT)
	public R<IPage<TopicUserVO>> page(TopicUserVO topicUser, Query query) {
		IPage<TopicUserVO> pages = topicUserService.selectTopicUserPage(Condition.getPage(query), topicUser);
		return R.data(pages);
	}

	/**
	 * @Description 新增 论坛板块用户关联表
	 * @Author daina
	 * @Date 15:58 2020/3/6
	 * @param topicUser
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiLog("新增 论坛板块用户关联表")
	@ApiOperation(value = "新增 论坛板块用户关联表", notes = "传入topicUser")
	@Log(title = "新增 论坛板块用户关联表",businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody TopicUser topicUser) {
		return R.status(topicUserService.save(topicUser));
	}

	/**
	 * @Description 修改 论坛板块用户关联表
	 * @Author daina
	 * @Date 15:58 2020/3/6
	 * @param topicUser
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改 论坛板块用户关联表", notes = "传入topicUser")
	@ApiLog("修改 论坛板块用户关联表")
	@Log(title = "修改 论坛板块用户关联表",businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody TopicUser topicUser) {
		return R.status(topicUserService.updateById(topicUser));
	}

	/**
	 * @Description 新增或修改 论坛板块用户关联表
	 * @Author daina
	 * @Date 15:59 2020/3/6
	 * @param topicUser
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改 论坛板块用户关联表", notes = "传入topicUser")
	@ApiLog("新增或修改 论坛板块用户关联表")
	@Log(title = "新增或修改 论坛板块用户关联表",businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody TopicUser topicUser) {
		return R.status(topicUserService.saveOrUpdate(topicUser));
	}

	
	/**
	 * @Description 删除 论坛板块用户关联表
	 * @Author daina
	 * @Date 15:59 2020/3/6
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	@ApiLog("删除")
	@Log(title = "删除", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(topicUserService.removeByIds(Func.toLongList(ids)));
	}
	/**
	 * @Description 论坛板块设置管理员(批量设置)
	 * @Author daina
	 * @Date 18:26 2020/3/4
	 * @param bbsTopicId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/setmanager")
	@ApiOperationSupport(order = 7)
	@ApiLog("论坛板块设置管理员")
	@ApiOperation(value = "论坛板块设置管理员", notes = "bbsTopicId,ids")
	@Log(title = "论坛板块设置管理员", businessType = BusinessType.INSERT)
	public R setmanager(@ApiParam(name = "bbsTopicId", value = "板块ID") @RequestParam Long bbsTopicId, @ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		TopicUser topicUserVO = new TopicUser();
		topicUserVO.setBbsTopicId(bbsTopicId);
		List<TopicUser> topicUserList = topicUserService.listTopicUser(topicUserVO);
		if (topicUserList.size() != 0){

			for(int i=0;i<topicUserList.size();i++){
				topicUserService.removeById(topicUserList.get(i).getId());
			}
		}
		List<Long> userIds = Func.toLongList(ids);
		ArrayList<TopicUser> list = new ArrayList<>();
		userIds.forEach(userId -> {
			TopicUser topicUser = new TopicUser();
			topicUser.setBbsTopicId(bbsTopicId);
			topicUser.setOpcUserId(userId);
			list.add(topicUser);
		});
		return R.status(topicUserService.saveOrUpdateBatch(list));
	}


}
